<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     rwd_enterprise
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */
?>
<?php /* @var $this Enterprise_GiftCard_Block_Catalog_Product_View_Type_Giftcard */?>
<?php $_product = $this->getProduct(); ?>
<?php $_id = $_product->getId(); ?>
<div class="giftcard-form">
<?php if ($this->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
        <p class="availability in-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('In stock') ?></span></p>
    <?php else: ?>
        <p class="availability out-of-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('Out of stock') ?></span></p>
    <?php endif; ?>
<?php endif; ?>

<?php echo $this->getChildHtml('product_type_data_extra') ?>

<?php if ($_product->isSaleable() && $this->isConfigured($_product)): ?>
    <?php if (!$this->isOpenAmountAvailable($_product) && (count($this->getAmounts($_product)) == 1)): ?>
        <?php $_amount = $this->getAmounts($_product); $_amount = array_shift($_amount); ?>
        <div class="price-box">
            <span class="regular-price">
                <span class="price" id="product-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
                    <?php echo Mage::helper('core')->currency($_amount, true, false); ?>
                </span>
            </span>
        </div>
        <script type="text/javascript">
        //<![CDATA[
            //add gift card amount as product price
            optionsPrice.productPrice = "<?php echo $_amount ?>";
        //]]>
        </script>

    <?php else: ?>
        <div class="fieldset giftcard-amount-form">
            <ul class="form-list">
                <li>
                    <?php if ($this->isAmountAvailable($_product)): ?>
                        <label for="giftcard_amount" id="amount_label_select" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount'); ?></label>
                    <?php endif; ?>
                    <?php if ($this->isOpenAmountAvailable($_product)): ?>
                        <label for="giftcard_amount_input" id="amount_label_input" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Amount in %s', $this->getCurrentCurrency()); ?></label>
                    <?php endif; ?>
                    <?php if ($this->isOpenAmountAvailable($_product)): ?>
                    <div id="giftcard_amount_box" class="input-box">
                        <span class="v-fix"><input type="text" id="giftcard_amount_input" name="custom_giftcard_amount" class="validate-number required-entry input-text giftcard-amount-entry giftcard-min-amount giftcard-max-amount" value="<?php echo $this->escapeHtml($this->getDefaultValue('custom_giftcard_amount')) ?>" /></span>

                        <?php if ($_product->getOpenAmountMin() || $_product->getOpenAmountMax()): ?>
                            <?php $_min = Mage::helper('core')->currency($_product->getOpenAmountMin(), true, false); ?>
                            <?php $_max = Mage::helper('core')->currency($_product->getOpenAmountMax(), true, false); ?>
                            <p class="notice">
                                <?php if ($_product->getOpenAmountMax() && $_product->getOpenAmountMin()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                <?php elseif ($_product->getOpenAmountMin()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Minimum: %s', $_min); ?></span>
                                <?php elseif ($_product->getOpenAmountMax()): ?>
                                    <span><?php echo Mage::helper('enterprise_giftcard')->__('Maximum: %s', $_max); ?></span>
                                <?php endif; ?>
                            </p>
                            <script type="text/javascript">
                            //<![CDATA[
                                minAllowedAmount = <?php echo Mage::helper('core')->currency($_product->getOpenAmountMin(), false, false) ?>;
                                maxAllowedAmount = <?php echo Mage::helper('core')->currency($_product->getOpenAmountMax(), false, false) ?>;

                                function parsePrice(value) {
                                    value = value.replace('\'', '').replace(' ', '');
                                    separatorComa = value.indexOf(',');
                                    separatorDot  = value.indexOf('.');

                                    if (separatorComa != -1 && separatorDot != -1) {
                                        if (separatorComa > separatorDot) {
                                            value = value.replace('.', '').replace(',', '.');
                                        } else {
                                            value = value.replace(',', '');
                                        }
                                    } else if (separatorComa != -1) {
                                        value = value.replace(',', '.');
                                    }

                                    return parseFloat(value);
                                }

                                Validation.add('giftcard-min-amount', '<?php echo Mage::helper('enterprise_giftcard')->__('Entered amount is too low'); ?>', function(v) {
                                    return (parsePrice(v) >= minAllowedAmount);
                                });
                                Validation.add('giftcard-max-amount', '<?php echo Mage::helper('enterprise_giftcard')->__('Entered amount is too high'); ?>', function(v) {
                                    if (maxAllowedAmount == 0) {return true;}
                                    return (parsePrice(v) <= maxAllowedAmount);
                                });
                            //]]>
                            </script>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    <?php if ($this->isAmountAvailable($_product)): ?>
                        <?php $configValue = $this->getDefaultValue('giftcard_amount'); ?>
                        <div class="input-box">
                            <select name="giftcard_amount" id="giftcard_amount" class="required-entry giftcard-amount-entry">
                                <option value=""><?php echo Mage::helper('enterprise_giftcard')->__('Choose an Amount...') ?></option>
                                <?php foreach ($this->getAmounts($_product) as $value): ?>
                                    <option value="<?php echo $value; ?>" <?php echo (($configValue == $value) ? 'selected' : '')?>><?php echo Mage::helper('core')->currency($value, true, false); ?></option>
                                <?php endforeach; ?>

                                <?php if ($this->isOpenAmountAvailable($_product)): ?>
                                    <option value="custom" <?php echo (($configValue == 'custom') ? 'selected' : '')?>><?php echo Mage::helper('enterprise_giftcard')->__('Other Amount...') ?></option>
                                <?php endif; ?>
                            </select>
                        </div>
                    <?php endif; ?>
                </li>
            </ul>
        </div>
        <script type="text/javascript">
        //<![CDATA[
        if ($('giftcard_amount') && $('giftcard_amount_input')) {
            $('giftcard_amount_input').hide();
            $('giftcard_amount_box').hide();
            $('amount_label_input').hide();
            Event.observe($('giftcard_amount'), 'change', switchGiftCardInputs);
        }

        function switchGiftCardInputs()
        {
            if (!$('giftcard_amount') || !$('giftcard_amount_input')) {
                return;
            }

            value = $('giftcard_amount').options[$('giftcard_amount').selectedIndex].value;
            if (value == 'custom') {
                $('giftcard_amount_input').show();
                $('giftcard_amount_box').show();

                $('amount_label_select').hide();
                $('amount_label_input').show();
            } else {
                $('giftcard_amount_input').hide();
                $('giftcard_amount_box').hide();

                $('amount_label_select').show();
                $('amount_label_input').hide();
            }
        }

        switchGiftCardInputs();
        //]]>
        </script>
    <?php endif; ?>
    <div class="fieldset giftcard-send-form">
        <ul class="form-list">
            <?php echo $this->getChildHtml('product.info.giftcard.form') ?>
            <li class="fields">
                <div class="field">
                    <label for="giftcard_recipient_name" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Name'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_recipient_name" name="giftcard_recipient_name" class="required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_recipient_name')) ?>" />
                    </div>
                </div>
                <?php if ($this->isEmailAvailable($_product)): ?>
                <div class="field">
                    <label for="giftcard_recipient_email" class="required"><em>*</em><?php echo Mage::helper('enterprise_giftcard')->__('Recipient Email'); ?></label>
                    <div class="input-box">
                        <input type="text" id="giftcard_recipient_email" name="giftcard_recipient_email" class="validate-email required-entry input-text" value="<?php echo $this->escapeHtml($this->getDefaultValue('giftcard_recipient_email')) ?>" />
                    </div>
                </div>
                <?php endif; ?>
            </li>
        <?php if ($this->isMessageAvailable($_product)): ?>
            <li>
                <label for="giftcard_message"><?php echo Mage::helper('enterprise_giftcard')->__('Message'); ?></label>
                <div class="input-box">
                    <textarea name="giftcard_message" id="giftcard_message" class="input-text giftcard-message-max-length" cols="5" rows="3"><?php echo $this->escapeHtml($this->getDefaultValue('giftcard_message')) ?></textarea>
                    <?php if ($this->getMessageMaxLength()): ?>
                        <script type="text/javascript">
                            Validation.add('giftcard-message-max-length', '<?php echo $this->jsQuoteEscape(Mage::helper('enterprise_giftcard')->__('Maximum length of the message is %d characters.', $this->getMessageMaxLength())); ?>', function(v) {
                                return v.length <= <?php echo $this->getMessageMaxLength(); ?>;
                            });
                        </script>
                    <?php endif; ?>
                </div>
            </li>
        <?php endif; ?>
        </ul>
    </div>
<?php elseif ($_product->isAvailable() && $this->isConfigured($_product)): ?>
    <?php echo $this->getPriceHtml($_product); ?>
<?php endif; ?>
</div>
